package com.ss.mq.pattern.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.ss.mq.util.RabbitUtils;

import java.util.Scanner;

/**
 * @author 叁昇
 * @create 2020-12-19-15:48
 */
public class TopicProducer {
    public static void main(String[] args) throws Exception {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();

        // 定义一个 topic 交换机
        channel.exchangeDeclare("topic_log", BuiltinExchangeType.TOPIC);

        while (true) {
            System.out.print("请输入消息: ");
            String msg = new Scanner(System.in).nextLine();
            if ("exit".equals(msg)) break;
            System.out.print("请输入路由键: ");
            String routingKey = new Scanner(System.in).nextLine();

            channel.basicPublish("topic_log", routingKey, null, msg.getBytes());

            System.out.println("消息已发送: [ "+ routingKey +" - "+msg +" ]");
        }
    }
}
